package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
/*
    @RequestMapping("/requestUrl1")
    public String requestUrl1(){
        return "page.jsp";
    }

    @RequestMapping("/showPage1")
    public String showPage1() {
        System.out.println("user mvc controller is running ...");
        return "forward:page.jsp";
    }

    @RequestMapping("/showPage2")
    public String showPage2() {
        System.out.println("user mvc controller is running ...");
        return "redirect:page.jsp";
    }

    @RequestMapping("/showPage3")
    public String showPage3() {
        System.out.println("user mvc controller is running ...");
        return "page";
    }

    //最简页面配置方式，使用访问路径作为页面名称，省略返回值
    @RequestMapping("/page")
    public void showPage5() {
        System.out.println("user mvc controller is running ...");
    }

    @RequestMapping("/showPageAndData1")
    public String showPageAndData1(HttpServletRequest request) {
        request.setAttribute("name","itheima");
        return "page";
    }*/

    @RequestMapping("/save")
    public String save(String name) {
        System.out.println("UserController is running ..." + name);
        return "success.jsp";
    }

    /**
     * @RequestParam的使用 类型： 形参注解
     * 作用：绑定请求参数与对应处理方法形参间的关系
     */
    @RequestMapping("/requestParam1")
    public String requestParam1(@RequestParam(
            name = "username",
            required = true
            //defaultValue = "itheima"
    ) String name) {
        System.out.println("name: " + name);
        return "page.jsp";
    }

    /**
     * 参数如果为int类型,建议使用Integer包装类
     */
    @RequestMapping("/requestParam2")
    public String requestParam2(String name, Integer age) {
        System.out.println("name=" + name + "," + "age=" + age);
        return "page.jsp";
    }
    //http://localhost/requestParam4?name=itheima&age=14

    /**
     * 当POJO类型属性与其他形参出现同名问题时，将被同时赋值 建议使用@RequestParam注解进行区分
     */
    @RequestMapping("/requestParam3")
    public String requestParam3(User user, @RequestParam(name = "newAge") Integer age) {
        System.out.println("用户的年龄=" + user.getAge() + ",age=" + age);
        System.out.println(user);
        return "page.jsp";
    }

    // http://localhost/requestParam5?address.province=beijing
    @RequestMapping("/requestParam4")
    public String requestParam4(User user) {
        System.out.println("user.address=" + user.getAddress().getProvince());
        return "page.jsp";
    }

    //http://localhost/requestParam7?addresses[0].province=bj&addresses[1].province=tj
    @RequestMapping("/requestParam5")
    public String requestParam5(User user) {
        System.out.println(user.getAddresses());
        return "page.jsp";
    }

    //http://localhost/requestParam8?addressMap[’home’].province=bj&addressMap[’job’].province=tj
    @RequestMapping("/requestParam6")
    public String requestParam6(User user) {
        System.out.println(user.getAddressMap());
        return "page.jsp";
    }

    /**
     * 请求参数名与处理器方法形参名保持一致，且请求参数数量＞ 1个
     * 访问URL： http://localhost/requestParam9?nick=Jockme&nick=zahc
     */
    @RequestMapping("/requestParam7")
    public String requestParam7(String[] nickName) {
        System.out.println(nickName[0] + " , " + nickName[1]);
        return "page.jsp";
    }

    /**
     * 保存简单类型数据，请求参数名与处理器方法形参名保持一致，且请求参数数量＞ 1个
     * 访问URL： http://localhost/requestParam10?nick=Jockme&nick=zahc
     */
    @RequestMapping("/requestParam8")
    public String requestParam8(@RequestParam("nickName") List<String> nickName) {
        System.out.println(nickName);
        return "page.jsp";
    }

    @RequestMapping("/requestParam9")
    public String requestParam9(Date date) {
        System.out.println(date);
        return "page.jsp";
    }

    /**
     * 名称：@DateTimeFormat
     * 类型:形参注解、成员变量注解
     * 作用:为当前参数或变量指定类型转换规则
     */
    /*@RequestMapping("/requestParam10")
    public String requestParam10(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        System.out.println(date);
        return "page.jsp";
    }*/
}
